#include "tool.h"

void Delay(uint32_t ms) {
    usleep(ms * 1000);
}

uint16_t Point_Distance(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
    int16_t dx = x2 - x1;
    int16_t dy = y2 - y1;
    return dx * dx + dy * dy;
}

Time Time_Make(uint32_t seconds) {
    Time time = {0, 0, 0};

    time.hour = seconds / 3600;
    time.minute = (seconds % 3600) / 60;
    time.second = (seconds % 3600) % 60;

    return time;
}

Time Time_MakeLocal() {
    time_t currentTime;
    time(&currentTime);

    struct tm *localTime = localtime(&currentTime);
    Time time = {0, 0, 0};

    // char timeString[80];
    // strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", localTime);
    // printf("%s\n", timeString);

    time.hour = localTime->tm_hour;
    time.minute = localTime->tm_min;
    time.second = localTime->tm_sec;
    return time;
}

uint32_t Time_ToSeconds(Time time) {
    return time.hour * 3600 + time.minute * 60 + time.second;
}
